package com.shopping_back.controller;


import com.shopping_back.common.Result;
import com.shopping_back.pojo.entity.Category;
import com.shopping_back.service.ICategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品类目表 前端控制器
 * </p>
 *
 * @author Jack
 * @since 2025-07-12
 */
@RestController
@RequestMapping("/api/category")
@RequiredArgsConstructor
public class CategoryController {
    final ICategoryService categoryService;

    @GetMapping("/categories")
    public Result getCategories() {
        List<Category> categories = categoryService.list();
        return Result.success(categories);
    }

    /** 管理员分页查询（可按名称模糊搜索、父ID筛选） */
    @GetMapping("/page")
    public Result page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long parentId,
            @RequestParam(required = false) String keyword) {

        return Result.success(categoryService.pageCategory(pageNum, pageSize, keyword));
    }

    /** 新增 */
    @PostMapping
    public Result create(@RequestBody @Validated Category param) {
        categoryService.createCategory(param);
        return Result.success();
    }

    /** 修改 */
    @PutMapping("/{id}")
    public Result update(@PathVariable Long id,
                         @RequestBody @Validated Category param) {
        param.setId(id);
        categoryService.updateCategory(param);
        return Result.success();
    }

    /** 删除（默认逻辑删除，如需物理删除可换 removeById） */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }
}
